import request from '@/utils/request'

// 作业模板参数
export interface AssignmentTemplate {
  id?: number | string
  title: string
  description?: string
  aiPromptTemplate?: string
  focusDirections?: string
  aiThinkingProcess?: string
  isPublished?: 0 | 1 // 0: 未发布, 1: 已发布
  createTime?: string
  updateTime?: string
  // AI模型配置参数
  modelName?: string
  temperature?: number
  maxTokens?: number
  filePath?: string // 文档路径
}

// 作业模板列表查询参数
export interface AssignmentTemplateListParams {
  current: number
  pageSize: number
  title?: string
  isPublished?: 0 | 1
  sortField?: string
  sortOrder?: string
}

// 生成AI阅卷思路参数
export interface GenerateAIThinkingParams {
  filePath: string;
  focusDirections: string;
  modelName?: string;
  temperature?: number;
  maxTokens?: number;
}

// 获取作业模板列表
export function getAssignmentTemplateList(params: AssignmentTemplateListParams) {
  return request.post<{
    code: number
    data: {
      total: number
      result: AssignmentTemplate[]
    }
    message: string
  }>('/assignmentTemplates/page', params)
}

// 获取作业模板详情
export function getAssignmentTemplate(id: number | string) {
  return request.get<{
    code: number
    data: AssignmentTemplate
    message: string
  }>(`/assignmentTemplates/get/${id}`)
}

// 创建作业模板
export function createAssignmentTemplate(data: AssignmentTemplate) {
  return request.post('/assignmentTemplates/create', data)
}

// 更新作业模板
export function updateAssignmentTemplate(data: AssignmentTemplate) {
  return request.post('/assignmentTemplates/update', data)
}

// 删除作业模板
export function deleteAssignmentTemplate(id: number | string) {
  return request.delete(`/assignmentTemplates/delete/${id}`)
}

// 生成AI阅卷思路 (标准请求，非流式)
export function generateAIThinking(data: GenerateAIThinkingParams) {
  return request.post<{
    code: number
    data: string
    message: string
  }>('/assignmentTemplates/generate-ai-thinking', data)
}

// 上传文件
export function uploadFile(file: File) {
  return request.upload<{
    code: number
    data: string | {
      url: string
      filename: string
    }
    message: string
  }>('/file/upload', file)
}

/**
 * 生成AI批阅思路 
 * @param data 请求参数
 * @returns 返回AI思考结果
 */
export function generateAIThinkingSimple(data: GenerateAIThinkingParams): Promise<string> {
  if (!data.focusDirections) {
    return Promise.reject(new Error('请输入教师关注点'));
  }

  // 确保使用正确的API调用路径
  return request.post<{
    code: number
    data: string
    message: string
  }>('/assignmentTemplates/generate-ai-thinking', data)
    .then(response => {
      if (response.code === 200 && response.data) {
        return response.data;
      } else {
        throw new Error(response.message || '获取AI思考过程失败');
      }
    });
} 